using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 登录历史管理控制器
    /// 提供登录历史的查询、统计等API接口
    /// 支持登录记录管理、安全审计、用户行为分析等功能
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class LoginHistoryController : ControllerBase
    {
        /// <summary>
        /// 登录历史应用服务接口
        /// </summary>
        private readonly ILoginHistoryAppService _service;
        /// <summary>
        /// 构造函数，注入登录历史服务
        /// </summary>
        /// <param name="service">登录历史应用服务接口</param>
        public LoginHistoryController(ILoginHistoryAppService service)
        {
            _service = service;
        }
    /// <summary>
        /// 获取所有登录历史记录
        /// </summary>
        /// <returns>登录历史列表</returns>
    [HttpGet]
        public async Task<IEnumerable<LoginHistoryDto>> GetAll()
        {
            return await _service.GetAllAsync();
        }
    /// <summary>
        /// 根据主键获取登录历史记录
        /// </summary>
        /// <param name="id">登录历史主键</param>
        /// <returns>登录历史数据</returns>
    [HttpGet("{id}")]
        public async Task<ActionResult<LoginHistoryDto>> GetById(long id)
        {
            var result = await _service.GetByIdAsync(id);
            if (result == null) return NotFound();
            return result;
        }
    /// <summary>
        /// 添加登录历史记录
        /// </summary>
        /// <param name="dto">登录历史数据传输对象</param>
        /// <returns>操作结果</returns>
    [HttpPost]
        public async Task<IActionResult> Add(LoginHistoryDto dto)
        {
            await _service.AddAsync(dto);
            return Ok();
        }
    /// <summary>
        /// 更新登录历史记录
        /// </summary>
        /// <param name="id">登录历史主键</param>
        /// <param name="dto">登录历史数据传输对象</param>
        /// <returns>操作结果</returns>
    [HttpPut("{id}")]
        public async Task<IActionResult> Update(long id, LoginHistoryDto dto)
        {
            await _service.UpdateAsync(id, dto);
            return Ok();
        }
    /// <summary>
        /// 删除登录历史记录
        /// </summary>
        /// <param name="id">登录历史主键</param>
        /// <returns>操作结果</returns>
    [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            await _service.DeleteAsync(id);
            return Ok();
        }
    }
}
